<?php

namespace app\company\controller;

use app\company\validate\WarehouseStorageGoodsLogsValidate;
use app\service\traits\Crud;
use app\mini\model\StoreOrderDetail;
use app\company\model\WarehouseStorageGoodsLogs as WarehouseStorageGoodsLogsModel;

class WarehouseStorageGoodsLogs extends Base
{
    private WarehouseStorageGoodsLogsModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new WarehouseStorageGoodsLogsModel();
        $this->validateClass = WarehouseStorageGoodsLogsValidate::class;
        $this->withSearch    = 'id,company_id,goods_id,goods_rule_id,supplier_id,create_time,type';
        $this->isGs          = true;
        $this->field         = 'id,goods_id,goods_rule_id,supplier_id,company_id,create_time,type,quantity,warehouse_storage_id,company_order_storage_id,amount,store_order_detail_id';
        $this->append        = 'warehouse_storage_goods_logs_type_text';
    }

    public function format($list): mixed
    {
        return $list->each(function ($item) {
            $item->goods_name             = $item->goods()->withTrashed()->value('name');
            $item->goods_spec_name        = $item->goodsRule()->withTrashed()->value('spec_name');
            $item->production_time        = setDates($item->companyOrderStorage()->withTrashed()->value('production_time'));//生产日期
            $item->expire_time            = setDates($item->companyOrderStorage()->withTrashed()->value('expire_time'));//到期日期
            $storage                      = $item->warehouseStorage()->find();
            $item->warehouse_storage_name = $storage->name;
            $item->warehouse_area_name    = $storage->warehouseArea()->withTrashed()->value('name');
            $item->warehouse              = $storage->warehouse()->field('name,real_name,mobile')->find();
            $item->amount                 = StoreOrderDetail::where('id',$item->store_order_detail_id)->value('money');
            $item->total_money            = bcmul($item->quantity, $item->amount, 2);
        });
    }

}